import { ref } from "vue";
import { jobGet } from "../api/pro";
import { JobType } from "../types/pro";

export const useJobList = () => {
  const jobList = ref<Array<JobType>>([]);
  const loading = ref(false); //当loading为false的时候，触底可以触发onLoad
  const finished = ref(false); //当数据全部请求完毕后，关闭触底监听
  let page = 1;
  // onLoad是由van-list组件自动触发
  const onLoad = () => {
    jobGet(page).then((res) => {
      let { results } = res.data;
      if (results.length) {
        jobList.value = [
          ...jobList.value, //原数据
          ...results, //新数据
        ];
        loading.value = false; //停止loading，保证下次可以继续触发onLoad
        page++;
      } else {
        finished.value = true; //所有数据加载完毕
      }
    });
    console.log("触底了");
  };
  return {
    loading,
    finished,
    jobList,
    onLoad,
  };
};
